package com.example.demo.controller;

import com.example.demo.AppUtil.ApiPageUtils;
import com.example.demo.AppUtil.BaseActionUtil;
import com.example.demo.AppUtil.QueryUtil;
import com.example.demo.anno.LoginUser;
import com.example.demo.entity.AfoneshopTopic;
import com.example.demo.entity.User;
import com.example.demo.service.IAfoneshopTopicService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @Author:大力王
 * @Date:2024/6/24-24-22:35
 * 开始了
 */
@Api(tags = "专题管理")
@RestController
@RequestMapping("/topic")
public class TopicController extends BaseActionUtil {
    @Resource
    private IAfoneshopTopicService topicService;
    @ApiOperation(value = "专题列表")
    @ApiOperationSupport(author = "Mr.力<import>器")
    @PostMapping("/list")
    public Object index(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Map<String, Object> param = new HashMap<>();
        param.put("page", page);
        param.put("limit", size);
        param.put("sidx", "id");
        param.put("order", "desc");
        param.put("fields", "id, title, price_info, scene_pic_url,subtitle");
        //查询列表数据
        QueryUtil query = new QueryUtil(param);
        List<AfoneshopTopic> topicEntities = topicService.queryList(query);
        int total = topicService.queryTotal(query);
        ApiPageUtils pageUtil = new ApiPageUtils(topicEntities, total, query.getLimit(), query.getPage());
        return toResponseSuccess(pageUtil);
    }
    @ApiOperation(value = "专题详情")
    @ApiOperationSupport(author = "Mr.力器")
    @PostMapping("/detail")
    public Object detail(Integer id) {
        AfoneshopTopic topicEntity = topicService.queryObject(id);
        return toResponseSuccess(topicEntity);
    }
    @ApiOperation(value = "关联专题")
    @ApiOperationSupport(author = "Mr.力器")
    @PostMapping("/related")
    public Object related(@LoginUser User loginUser, Integer id){
        Map<String, Object> param = new HashMap<>();
        param.put("limit", 4);
        List<AfoneshopTopic> topicEntities = topicService.queryList(param);
        return toResponseSuccess(topicEntities);
    }
}
